﻿using DeHeng_Mes.Model.ViewModel;
using DeHeng_Mes.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DeHeng_Mes.Dao
{
    public class ViewDao
    {
        /// <summary>
        /// 上件第一工序结束插入视图数据
        /// </summary>
        /// <param name="lineMesData"></param>
        public void InsertData(LineMesData lineMesData)
        {
            using (var db = ViewSugarConfig.Db)
            {
                db.Insertable(lineMesData).ExecuteCommand();
            }
        }

        /// <summary>
        /// 插入工件上件数据
        /// </summary>
        /// <param name="lineMesData"></param>
        public async Task<int> PushPartData(LineMesData lineMesData)
        {
            using (var db = ViewSugarConfig.Db)
            {
                int count = await db.Insertable(lineMesData).ExecuteCommandAsync();

                if (count > 0)
                {
                    Tools.ProduceLogText($"推送一条工位绑定数据成功，数据标识[{lineMesData.DataId}]，工件号[{lineMesData.TraceId}]");
                }

                return count;
            }
        }

        /// <summary>
        /// 插入过站数据
        /// </summary>
        /// <param name="lineMesDataTrace"></param>
        public async Task<int> PushStationData(LineMesDataTrace lineMesDataTrace)
        {
            using (var db = ViewSugarConfig.Db)
            {
                int count = await db.Insertable(lineMesDataTrace).ExecuteCommandAsync();

                if (count > 0)
                {
                    Tools.ProduceLogText($"推送一条过站数据成功，数据标识[{lineMesDataTrace.DataId}]，唯一过站标识[{lineMesDataTrace.DataTraceId}]，工位号[{lineMesDataTrace.Station}]");
                }

                return count;
            }
        }

        /// <summary>
        /// 插入投料数据
        /// </summary>
        /// <param name="lineMesDataDtl"></param>
        public async Task<int> PushBatchData(LineMesDataDtl lineMesDataDtl)
        {
            using (var db = ViewSugarConfig.Db)
            {
                int count = await db.Insertable(lineMesDataDtl).ExecuteCommandAsync();

                if (count > 0)
                {
                    Tools.ProduceLogText($"推送一条投料明细数据成功，数据标识[{lineMesDataDtl.DataId}]，唯一投料标识[{lineMesDataDtl.DataDtlId}]");
                }

                return count;
            }
        }
    }
}
